package com.powermgr.boringtest.scheduled;

import com.alibaba.fastjson.JSON;
import com.powermgr.boringtest.bean.VisitData;
import com.powermgr.boringtest.service.IStartService;
import com.powermgr.boringtest.service.IVisitService;
import com.powermgr.boringtest.utils.DailyUtils;
import com.powermgr.boringtest.utils.FileZipUtils;
import com.powermgr.boringtest.utils.ReadWriteUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import java.util.List;
import java.util.stream.Collectors;

import static com.powermgr.boringtest.utils.ConfigUtils.*;

/**
 * ScheduledTask
 *
 * @author qiuxiangdong
 * @date 2023/6/12
 */
@Log4j2
@Component
public class ScheduledTask {
    @Resource
    private IVisitService accessService;
    @Resource
    private IStartService startService;

    @Scheduled(cron = "0 0 2 * * ?")//每天2点获取最新的master用例
    public void getDaily(){
        FileZipUtils.deleteFile(DAILY);
        FileZipUtils.createFile(DAILY);
        DailyUtils.getDaily();
    }

    @Scheduled(cron = "0 0 0,8,10,12,14,16,18,20,22 * * *")//每天定点保存统计信息
    public void persistAccess() {
        try {
            VisitData visitData = accessService.getVisitData();
            String accessStr = JSON.toJSONString(visitData);
            ReadWriteUtils.writeFile(VISIT_JSON, accessStr);
            log.info("保存统计信息");
        } catch (IOException e) {
            log.error("保存统计信息失败", e);
        }
    }

    @Scheduled(cron = "0 0 5 * * ?")//每天5点存档超过一周的 pr json 文件
    public void cleanPrJson(){
        try {
            Path prPath = Paths.get(PR_3DAY);
            List<Path> prs = Files.list(prPath).collect(Collectors.toList());
            for (Path pr : prs) {
                String fileName = pr.getFileName().toString();
                Path archivePath = Paths.get(PR_LIST);
                FileTime lastModifiedTime = Files.getLastModifiedTime(pr);
                FileTime sevenDaysAgo = FileTime.fromMillis(System.currentTimeMillis() - SEVEN_DAY);
                if (lastModifiedTime.compareTo(sevenDaysAgo) < 0 && fileName.endsWith(".json")) {
                    log.info("存档7天前的 " + pr.getFileName());
                    org.apache.commons.io.FileUtils.moveFileToDirectory(pr.toFile(), archivePath.toFile(), false);
                }
            }
            PR_FILE_FLAGS = true;
            startService.getList();
        } catch (IOException e) {
            log.info("存档7天前的 pr.json 文件 失败了", e);
        }
    }
    @Scheduled(cron = "0 0 4 * * ?")//每天4点清理超过一周的版本文件
    public void clean7Day(){
        try {
            Path prPath = Paths.get(PR_VERSION);
            delete(prPath);
        } catch (IOException e) {
            log.info("删除7天前的版本下载文件失败了", e);
        }
        try {
            Path prPath = Paths.get(PR_TDD);
            delete(prPath);
        } catch (IOException e) {
            log.info("删除7天前的tdd下载文件失败了", e);
        }
        try {
            Path prPath = Paths.get(PR_XTS);
            delete(prPath);
        } catch (IOException e) {
            log.info("删除7天前的xts下载文件失败了", e);
        }
        try {
            Path prPath = Paths.get(PR_REPORTS);
            delete(prPath);
        } catch (IOException e) {
            log.info("删除7天前的报告文件失败了", e);
        }
    }

    private void delete(Path prPath) throws IOException {
        List<Path> prs = Files.list(prPath).collect(Collectors.toList());
        for (Path pr : prs) {
            String fileName = pr.getFileName().toString();
            FileTime lastModifiedTime = Files.getLastModifiedTime(pr);
            FileTime sevenDaysAgo = FileTime.fromMillis(System.currentTimeMillis() - SEVEN_DAY);
            if (lastModifiedTime.compareTo(sevenDaysAgo) < 0 && fileName.endsWith(".gz")) {
                log.info("删除7天前下载文件 " + pr.getFileName());
                org.apache.commons.io.FileUtils.delete(pr.toFile());
            }
        }
    }
}